package product

import (
	"context"
	"errors"
	"group-buy/group_buy/internal/models"

	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetProductByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetProductByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductByIdLogic {
	return &GetProductByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetProductByIdLogic) GetProductById(req *types.GetProductByIdReq) (resp *types.Product, err error) {
	product := &models.Product{Id: req.Id}

	get, err := l.svcCtx.Engine.Get(product)
	if err != nil {
		return nil, err
	}
	if !get {
		return nil, errors.New("产品不存在")
	}

	return &types.Product{
		Id:             product.Id,
		Name:           product.Name,
		Price:          product.Price,
		Unit:           product.Unit,
		Cover:          product.Cover,
		Detail:         product.Detail,
		BusinessId:     product.BusinessId,
		WriteOff:       product.WriteOff,
		Status:         product.Status,
		WriteOffUserId: product.WriteOffUserId,
	}, nil
}
